---
--类名: EnterBattleView.lua
--创建日期: 2018-8-31
--创建人: 宋建邦
--功能描述:进入战斗视图
--修改内容:
--修改日期:
--修改人:
--修改备注:

local NewguildIconRender = require("app.newGuild.views.renders.NewguildIconRender")

local EnterBattleView = class("EnterBattleView", app.mvc.ViewBase)
EnterBattleView.VIEW_ROOT_RES = Res.EnterBattleView

function EnterBattleView:init()
    self:setBackGroundColorType(0)
    self:setNodeEventEnabled(true)

    self.mapName = self._root:findChild("Panel/tlt_name")
    self.mapIcon = self._root:findChild("Panel/map")
    self.boomTxt = self._root:findChild("Panel/tlt_boom_num")

    self.leftGuildName = self._root:findChild("Panel/Panel_guild/name_1")
    self.rightGuildName = self._root:findChild("Panel/Panel_guild/name_2")

    self.leftGuildIcon = NewguildIconRender.create(self._root:findChild("Panel/Panel_guild/icon_1"))
    self.rightGuildIcon = NewguildIconRender.create(self._root:findChild("Panel/Panel_guild/icon_2"))

    self.tips = self._root:findChild("Panel/tips")
    self.wndTlt = self._root:findChild("Panel/tlt")

    self.btnClose = self._root:findChild("Panel/Button_close")
    self.btnCloseName = self._root:findChild("Panel/Button_close/name")
    self.btnClose:addClickEventListener(function()
        local bc = app:getInst("BiddingController")
        if app:sendMsg("DynastyMapModel", "isDynastyMap") then -- 已经在公会战场
            display.pushToast(L("lua_code_text_273"))
        else
            local npcId = tonumber(self._data.config._conf.npcID)
            local districtId = tonumber(self._data.config._conf.ID)
            EM:notify("onTrackingWay",{trackNpcId=npcId, autoStartEvents=false, isTracking=true,immediately = true})
            -- 设置公会战追踪数据，追踪到的时候直接打开
            FieldManager:setTrackData(Enums.trackType.dynasty, districtId)
        end
        bc:closeEnterBattleView()
    end)

    display.nodeRemoveForNotHit(self._root:findChild("Panel"), function()
        local bc = app:getInst("BiddingController")
        bc:closeEnterBattleView()
    end)

    local act = cca.seq({cca.delay(30), cca.callFunc(function()
        self:refreshTime()
    end), })

    self.btnClose:runAction(act)
end

function EnterBattleView:onExit()
end

function EnterBattleView:refresh(data)
    self._data = data
    if self._data then
        local cfg = self._data.config
        local maxProsperity = 0
        if cfg then
            self.mapName:setString(cfg:getName())
            self.mapIcon:loadTexture(cfg:getIcon())
            maxProsperity = cfg.blessExpMax
        end

        local data = self._data.data
        if data then
            self.boomTxt:setString(string.format(L("lua_code_text_229"), data.prosperity, maxProsperity))
            local bc = app:getInst("BiddingController")
            local l = data.atk
            local r = data.def
            local myname = bc:myGuildName()
            if myname then
                if data.def and data.def.guildName == myname then
                    l = data.def
                    r = data.atk
                end
            end

            self:setFlag(self.leftGuildIcon, l)
            self:setFlag(self.rightGuildIcon, r)

            if l then self.leftGuildName:setString(l.guildName) end
            if r then self.rightGuildName:setString(r.guildName) end
            if bc:canEnterTown(data.id) then
                self.btnCloseName:setString(L("bidding_battle_enter"))
            else
                self.btnCloseName:setString(L("bidding_battle_watch"))
            end
        end

        self:refreshTime()
    end
end

function EnterBattleView:refreshTime()
    if self._data and self._data.data then
        local data = self._data.data
        local s = protobuf.enum_id("pb_town.PHASE", data.phase)
        local str = L("lua_code_text_58")
        if s == 2 then
            if data.fightStartTime then
                local ms = math.ceil(math.max(data.fightStartTime - app.session:getServerTime(), 0) / 60)
                -- str = L("战斗将在")..tostring(ms)..L("分钟后开始")
                str = string.format(L("bidding_battle_begin_time"), tostring(ms))
            end
            self.wndTlt:setString(L("bidding_battle_ready"))
        else
            if data.fightEndTime then
                local ms = math.ceil(math.max(data.fightEndTime - app.session:getServerTime(), 0) / 60)
                -- str = L("战斗将在")..tostring(ms)..L("分钟后结束")
                str = string.format(L("bidding_battle_end_time"), tostring(ms))
            end
            self.wndTlt:setString(L("bidding_battle_fighting"))
        end
        self.tips:setString(str)--或结束，取决于状态
    end
end

function EnterBattleView:setFlag(n, gi)
    if not gi then return end
    local param = {
        baseIconId = gi.flagBaseIcon,
        baseIconColor = gi.flagBaseIconColor,
        additionIconId = gi.flagAdditionalIcon,
        additionIconColor = gi.flagAdditionalIconColor,}
    n:setData(param)
end

return EnterBattleView